//
//  BaseTabBarController.m
//  Learn
//
//  Created by icomexe on 2018/10/27.
//  Copyright © 2018年 cjg. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseTabBarModel.h"
#import "BaseTabBar.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "WorkViewController.h"
#import "BaseNavigationViewController.h"

@interface BaseTabBarController ()<BaseTabBarDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) BaseTabBar *tarbarView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray <BaseTabBarModel *>*modelArray;
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, strong) BaseNavigationController *mineVC;
@property (nonatomic, strong) WorkViewController *workVC;

@end

@implementation BaseTabBarController
- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
        _scrollView.contentSize = CGSizeMake(SCREEN_W*self.modelArray.count, 0);
        _scrollView.delegate = self;
        _scrollView.scrollEnabled = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
    return _scrollView;
}

- (BaseTabBar *)tarbarView {
    
    if (!_tarbarView) {
        _tarbarView = [[BaseTabBar alloc] initWithFrame:CGRectMake(0, SCREEN_H-60-SafeAreaBottomHeight, SCREEN_W, 60+SafeAreaBottomHeight)];
        _tarbarView.delegate = self;
        _tarbarView.modelArray = self.modelArray;
    }
    return _tarbarView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self getdata];
    [self setupVCs];
}

- (void)getdata {
    
    NSArray *ary = @[
                     @{
                         @"title":@"首页",
                         @"defultImage":@"homeDefult",
                         @"selectedImage":@"homeSelect"
                         },
                     @{
                         @"title":@"作业",
                         @"defultImage":@"workDefult",
                         @"selectedImage":@"workSelect"
                         },
                     @{
                         @"title":@"我",
                         @"defultImage":@"mineDefult",
                         @"selectedImage":@"mineSelect"
                         }
                     ];
    
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dict in ary) {
        BaseTabBarModel *model = [BaseTabBarModel modelWithDict:dict];
        [arrM addObject:model];
    }
    self.modelArray = [arrM copy];
}

- (void)setupVCs {
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.tarbarView];
    
    //解决scrollview内容视图下移20像素的问题
    if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.mineVC = ({
        BaseNavigationController *Nav = [[BaseNavigationController alloc] initWithRootViewController:[[MineViewController alloc] init]];
        Nav;
    });
    self.homeVC = ({
        HomeViewController *vc = [[HomeViewController alloc] init];
        vc;
    });
    self.workVC = ({
        WorkViewController *vc = [[WorkViewController alloc] init];
        vc;
    });

    // 添加子控制器
    [self addChildViewController:[[BaseNavigationViewController alloc] initWithRootViewController:self.homeVC]];
    [self addChildViewController:self.workVC];
    [self addChildViewController:self.mineVC];
    // 先将第一个子控制的view添加到scrollView上去
    [self.scrollView addSubview:self.homeVC.view];
    self.tarbarView.currentIndex = 0;
}

#pragma mark ---- tarbar代理
- (void)baseTabBarDidSelectIndex:(NSUInteger)index {
    
    [self.scrollView setContentOffset:CGPointMake(SCREEN_W*index,0) animated:NO];
    NSLog(@"当前的坐标 －－－－－－－－－－－－－－－－－－－ %lu",(unsigned long)index);
    if (index == 0) {
        self.homeVC = self.childViewControllers[index];
        // 如果已经加载过，就不再加载
        if ([self.homeVC isViewLoaded]) return;
        self.homeVC.view.frame = CGRectMake(SCREEN_W*index, 0, SCREEN_W, SCREEN_H);
        [self.scrollView addSubview:self.homeVC.view];
    }
    if (index == 2) {
        self.mineVC = self.childViewControllers[index];
        // 如果已经加载过，就不再加载
        //if ([self.mineVC isViewLoaded]) return;
        self.mineVC.view.frame = CGRectMake(SCREEN_W*index, 0, SCREEN_W, SCREEN_H);
        [self.scrollView addSubview:self.mineVC.view];
    }
}

//获取当前的window
- (UIWindow *)mainWindow
{
    UIApplication *app = [UIApplication sharedApplication];
    return [app keyWindow];
}

@end
